1 using System.Collections;
2 using
System.Collections.Generic;
3 using
UnityEngine;
4
5 public
enum PlayerType {
6         P1, P2
7 }

8
9 public
class GCPlayer : IClicker, IInputReceiver {
10
11     
private PlayerType type;
12
13     
private List<Piece> pieces;
14     
private List<Piece> eatenPieces;
15
16     
private Piece piece;
17     
private Piece checkedBy; //Experimental
18
19     
//Experimental
20     
public bool IsChecked {
21         
get {return checkedBy != null;}
22     }
23
24     
public List<Piece> Pieces {
25         
get {return pieces;}
26     }
27
28     
public List<Piece> EatenPieces {
29         
get {return eatenPieces;}
30     }
31
32     
//Experimental
33     
public Piece CheckedBy {
34         
get {return checkedBy;}
35         
set {
36             checkedBy =
value;
37         }
38     }
39
40     
public Piece HoldingPiece {
41         
get {return piece;}
42     }
43
44     
public bool IsReady {
45         
get {
46             
for (int i = 0; i < pieces.Count; i++) {
47                 
if (!pieces[i].IsReady) return false;
48             }
49
50             
return true;
51         }
52     }
53
54     
public PlayerType Type {
55         
get {return type;}
56     }
57
58     
public GCPlayer(PlayerType type) {
59         
this.type = type;
60         pieces =
new List<Piece>();
61         eatenPieces =
new List<Piece>();
62     }
63
64     
public void EnableInput() {
65         InputManager.InputEvent += OnInputEvent;
66     }
67
68     
public void DisableInput() {
69         InputManager.InputEvent -= OnInputEvent;
70     }
71
72     
void OnDisable() {
73         DisableInput();
74     }
75
76     
public void OnInputEvent(InputActionType action) {
77         
switch (action) {
78             
case InputActionType.GRAB_PIECE:
79                 Node gNode = Finder.RayHitFromScreen<Node>(Input.mousePosition);
80                 
if (gNode == null) break;
81                 piece = gNode.Piece;
82                 
if (piece == null) break;
83                 
if (!piece.IsReady) break;
84                 
if (Click(gNode) && piece && Has(piece) && Click(piece)) {
85                     piece.Pickup();
86                     piece.Compute();
87                     piece.HighlightPossibleMoves();
88                     piece.HighlightPossibleEats();
89                     GameManager.Instance.GameState.Grab();
90                 }
91
92                 
//check clickable for tile and piece then pass Player
93                 
//check if player has piece - PIECE
94                 
//check if player has piece if not empty - NODE
95                 
break;
96             
case InputActionType.CANCEL_PIECE:
97                     
if (piece != null) {
98                         
//if (!piece.IsReady) break;
99                         piece.Drop();
100                         piece =
null;
101                         GameManager.Instance.GameState.Cancel();
102                     }
103                 
break;
104             
case InputActionType.PLACE_PIECE:
105                 Node tNode = Finder.RayHitFromScreen<Node>(Input.mousePosition);
106                 
if (tNode == null) break;
107                 Piece tPiece = tNode.Piece;
108                 
if (tPiece == null) {
109                     
if (piece.IsPossibleMove(tNode)) {
110                         
if (Rules.IsCheckMove(this,piece,tNode, true)) {
111                             Debug.Log(
"Move checked"); // do nothing
112                         }
else {
113                             piece.MoveToXZ(tNode, Drop);
114                             GameManager.Instance.GameState.Place();
115                         }
116                     }
117                 }
else {
118                     
if (piece.IsPossibleEat(tNode)) {
119                         
if (Rules.IsCheckEat(this,piece,tNode, true)) {
120                             Debug.Log(
"Eat checked"); // do nothing
121                         }
else {
122                             GCPlayer oppPlayer = GameManager.Instance.Opponent(
this);
123                             oppPlayer.RemovePiece(tPiece);
124                             AddEatenPieces(tPiece);
125                             tPiece.ScaleOut(
0.2f, 1.5f);
126                             piece.MoveToXZ(tNode, Drop);
127                             GameManager.Instance.GameState.Place();
128                         }
129                     }
130                 }
131                 
break;
132         }
133     }
134
135     
public void ClearPiecesPossibles() {
136         
for (int i = 0; i < pieces.Count; i++) {
137             pieces[i].ClearPossibleEats();
138             pieces[i].ClearPossibleMoves();
139         }
140     }
141
142     
public void ClearCheck() {
143         
if (checkedBy == null) return;
144         checkedBy =
null;
145         
//checkedBy.ClearCheck(this);
146     }
147
148     
//the methods inside must be in order
149     
private void Drop() {
150         piece.Drop();
151         piece.Compute();
152         GameManager.Instance.GameState.Release();
153         piece =
null;
154     }
155
156     
public bool Has(Piece piece) {
157         
return pieces.Contains(piece);
158     }
159
160     
public bool Click(IClickable clickable) {
161         
if (clickable == null) return false;
162         
return clickable.Inform<GCPlayer>(this);
163     }
164
165     
public void AddPieces(params Piece[] pieces) {
166         
for (int i = 0; i < pieces.Length; i++) {
167             
this.pieces.Add(pieces[i]);
168         }
169     }
170
171     
public void AddEatenPieces(params Piece[] pieces) {
172         
for (int i = 0; i < pieces.Length; i++) {
173             
this.eatenPieces.Add(pieces[i]);
174         }
175     }
176
177     
public bool RemovePiece(Piece piece) {
178         
return pieces.Remove(piece);
179     }
180
181     
public void ComputePieces() {
182         
for (int i = 0; i < pieces.Count; i++) {
183             pieces[i].Compute();
184         }
185     }
186 }


private Piece checkedBy; Experimental

Experimental

Experimental

check clickable for tile and piece then pass Player

check if player has piece - PIECE

check if player has piece if not empty - NODE

if (!piece.IsReady) break;

Debug.Log("Move checked"); do nothing

Debug.Log("Eat checked"); do nothing

checkedBy.ClearCheck(this);

the methods inside must be in order



Gõ tìm kiếm nhanh...